//浅拷贝的实现

let a = [1, [2], 3]
//通过slice或者concat拷贝数组
let b = a.slice(0)
//通过展开运算符拷贝数组a
let c = [...a]
//通过object.assign方法实现对象或数组的浅拷贝
let d = Object.assign([], a)


//深拷贝的实现
//json方式
let f = JSON.parse(JSON.stringify(a))  //存在缺点 拷贝对象包含 正则表达式，函数，或者undefined等值会失败

//递归方式
function deep(object) {
  let copy = object instanceof Array ? [] : {}
  for (let i in object) {
    if (object.hasOwnProperty(i)) {
      copy[i] = typeof (object[i]) == "object" ? deep(object[i]) : object[i]
    }
  }
  return copy
}
let g = deep(a)
a[1][0] = 3
console.log(a)
console.log(g)